package com.dmai.capacity.platform.user.controller;


import com.dmai.capacity.platform.common.dto.ResponseEntity;
import com.dmai.capacity.platform.user.model.SysArea;
import com.dmai.capacity.platform.user.model.SysOrganization;
import com.dmai.capacity.platform.user.pojo.TreeVo;
import com.dmai.capacity.platform.user.service.ISysAreaService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.list.TreeList;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 作者 ftquan E-mail: 464725602@qq.com
 * @version 创建时间：2017年11月12日 上午22:57:51
 * 区域管理
 */
@Slf4j
@RestController
public class SysAreaController {


    @Autowired
    private ISysAreaService areaService;


    /**
     * 查询区域
     *
     * @param params
     * @return
     */
//    @PreAuthorize("hasAuthority('area:get/areas')")
    @GetMapping("/areas")
    public ResponseEntity<List<SysArea>> findAreas(@RequestParam Map<String, Object> params) {
        List<SysArea> list = areaService.findAreas(params);
        return ResponseEntity.succeed(list);
    }

    /**
     * 查询区域tree
     *
     * @param parentId
     * @return
     */
//    @PreAuthorize("hasAuthority('area:get/areas')")
    @GetMapping("/areas/tree")
    public ResponseEntity<List<TreeVo>> treeAreas(@RequestParam(value = "parentId", defaultValue = "-1") String parentId) {
        List<SysArea> areas = areaService.findByParentId(parentId);
        List<TreeVo> treeList = new TreeList();
        areas.forEach(m -> {
            TreeVo node = new TreeVo();
            node.setId(m.getId());
            node.setText(m.getAreaName());
            node.setValue(m.getId().toString());
            node.setShowcheck(false);
            node.setCheckstate(0);
            node.setIsexpand(false);
            node.setComplete(false);
            node.setParentId(m.getParentId());
            List<SysArea> childs = areaService.findByParentId(node.getId());
            node.setHasChildren(!CollectionUtils.isEmpty(childs));

            treeList.add(node);
        });

        return ResponseEntity.succeed(treeList);
    }

//    /**
//     * 层级结构
//     *
//     * @param parentId
//     * @return
//     */
//    @GetMapping("/organizations/tree")
////    @PreAuthorize("hasAnyAuthority('sys_organization:sysorganization:list')")
//    public ResponseEntity<List<TreeVo>> tree(@RequestParam(value = "parentId", defaultValue = "-1") String parentId,
//                                             @RequestParam(value = "recursion", defaultValue = "0") Integer recursion) {
//        Map<String, Object> params = new HashMap<>();
//        params.put("status", 1);
//        if (0 == recursion) {
//            params.put("parentId", parentId);
//        }
//        List<SysOrganization> companys = areaService.findAll(params);
//        List<TreeVo> treeList = new TreeList();
//        companys.forEach(m -> {
//            TreeVo node = new TreeVo();
//            node.setId(m.getId());
//            node.setText(m.getName());
//            node.setTitle(m.getName());
//            node.setValue(m.getId().toString());
//            node.setShowcheck(false);
//            node.setCheckstate(0);
//            if (StringUtils.isEmpty(m.getParentId())) {
//                node.setIsexpand(true);
//            } else {
//                node.setIsexpand(false);
//            }
//            node.setComplete(false);
//            node.setParentId(m.getParentId());
//            treeList.add(node);
//        });
//
//        return ResponseEntity.succeed(TreeVo.RecursiveBuilder(treeList, parentId));
//    }


    /**
     * 查询单条记录
     */
    @GetMapping("/areas/{id}")
//    @PreAuthorize("hasAnyAuthority('area:areas:list')")
    public ResponseEntity findById(@PathVariable(value = "id") String id) {
        return ResponseEntity.succeed(areaService.findById(id));
    }

    /**
     * 保存
     */
    @PostMapping("/areas")
//    @PreAuthorize("hasAnyAuthority('area:areas:save')")
    public ResponseEntity save(@RequestBody @Valid SysArea area) {
        areaService.save(area);
        return ResponseEntity.succeed("操作成功");
    }

    /**
     * 修改
     */
    @PutMapping("/areas")
//    @PreAuthorize("hasAnyAuthority('area:areas:save')")
    public ResponseEntity update(@RequestBody @Valid SysArea area) {
        areaService.update(area);
        return ResponseEntity.succeed("操作成功");
    }

    /**
     * 删除区域
     * delete /areas/1
     *
     * @param id
     */
//    @PreAuthorize("hasAuthority('area:delete/areas/{id}')")
    @DeleteMapping("/areas/{id}")
    public ResponseEntity delete(@PathVariable(value = "id") String id) {
        try {
            areaService.delete(id);
            return ResponseEntity.succeed("操作成功");
        } catch (Exception e) {
            log.error(String.valueOf(e));
            return ResponseEntity.failed("操作失败");
        }
    }

}
